import os
from menu_table import MenuTable
from sys_config_reader import SysConfigReader
from user_action import UserAction,ServerHandler
from server_config import SysServerConfig

SSH_LOGIN_HOME = "SSH_LOGIN_LINUX_HOME"

class ProjectMain(object):

    def initialize(self):
        """
        初始化
        1: 切换工作目录
        """
        home_space = os.environ.get(SSH_LOGIN_HOME)
        if(home_space is None):
            print("warn: not set home_space environment variable")
            home_space = os.getcwd()
        os.chdir(home_space)

    def load_config(self):
        """加载配置"""
        config_reader = SysConfigReader()
        config_reader.load_sys_config();
        config_reader.print_banner()

        # 加载服务器配置
        sys_server_config = SysServerConfig(config_reader)
        serverList = sys_server_config.load()

        # 加载菜单配置
        menu_config = config_reader.menu_data_config();

        return serverList,menu_config,config_reader;

    def start(self,serverList,menu_config,sys_reader:SysConfigReader):
            
            menu = MenuTable(serverList,menu_config)
            action = UserAction(menu)

            while(action.start):

                action.envChoose()
                action.inputZeroHandler()

                server = action.serverChoose()
                if(action.level == 1):
                    pass
                if(server == None):
                    pass
                else:
                    ser_hander = ServerHandler(sys_reader=sys_reader)
                    ser_hander.handler(server)

if __name__ == '__main__':
    main = ProjectMain()
    
    main.initialize()
    project_config = main.load_config();
    # 启动
    main.start(project_config[0],project_config[1],project_config[2])




        
